package admin_api

import (
	"github.com/gin-gonic/gin"
	"shop/admin/admin_service"
	"shop/entity"
	"shop/utils/convertutil"
	"shop/utils/result"
	"shop/utils/sessionutil"
)

func GetGoodsCategoryList(c *gin.Context) {
	result.RenderResult(c, admin_service.GetGoodsCategoryList(sessionutil.GetShopIdFromCtx(c)))
}

func AddGoodsCategory(c *gin.Context) {
	goodsCategory := new(entity.GoodsCategory)
	if err := c.ShouldBindJSON(goodsCategory); err != nil {
		result.RenderFail(c, "参数格式错误："+err.Error())
		return
	}

	result.RenderResult(c, admin_service.AddGoodsCategory(sessionutil.GetShopIdFromCtx(c), goodsCategory))
}

func UpdateGoodsCategory(c *gin.Context) {
	goodsCategory := new(entity.GoodsCategoryFilter)
	if err := c.ShouldBindJSON(goodsCategory); err != nil {
		result.RenderFail(c, "参数格式错误："+err.Error())
		return
	}

	result.RenderResult(c, admin_service.UpdateGoodsCategory(convertutil.ParseInt(c.Param("id")), sessionutil.GetShopIdFromCtx(c), goodsCategory))
}

func DeleteGoodsCategory(c *gin.Context) {
	id := convertutil.ParseInt(c.Param("id"))
	result.RenderResult(c, admin_service.DeleteGoodsCategory(id, sessionutil.GetShopIdFromCtx(c)))
}
